package com.sunday.scoremanager.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sunday.common.adapter.CommenAdapter;
import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.CircleImageView;
import com.sunday.common.widgets.ClearEditText;
import com.sunday.common.widgets.MyRatingBar;
import com.sunday.common.widgets.NoScrollGridView;
import com.sunday.scoremanager.R;
import com.sunday.scoremanager.base.BaseApp;
import com.sunday.scoremanager.entity.Comment;
import com.sunday.scoremanager.http.ApiClient;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.sunday.scoremanager.base.Constants.TEST_URL;


/**
 * Created by 刘涛 on 2017/10/27.
 */

public class ReplyCommentActivity extends BaseActivity {

    @Bind(R.id.left_btn)
    ImageView leftBtn;
    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.rightTxt)
    TextView rightTxt;
    @Bind(R.id.right_btn)
    ImageView rightBtn;
    @Bind(R.id.common_header)
    RelativeLayout commonHeader;
    @Bind(R.id.user_logo)
    CircleImageView userLogo;
    @Bind(R.id.user_name)
    TextView userName;
    @Bind(R.id.rating_bar)
    MyRatingBar ratingBar;
    @Bind(R.id.user_content)
    TextView userContent;
    @Bind(R.id.btn_check_more)
    TextView btnCheckMore;
    @Bind(R.id.grid_view)
    NoScrollGridView gridView;
    @Bind(R.id.comment_time)
    TextView commentTime;
    @Bind(R.id.edit_reply)
    ClearEditText editReply;
    @Bind(R.id.btn_reply)
    TextView btnReply;
    @Bind(R.id.ll_reply)
    LinearLayout llReply;
    @Bind(R.id.reply_content)
    TextView replyContent;
    @Bind(R.id.ll_make_reply)
    LinearLayout llMakeReply;

    private Comment comment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reply_comment);
        ButterKnife.bind(this);
        comment = (Comment) getIntent().getSerializableExtra("comment");
        initView();
    }

    private void initView() {
        titleView.setText("店铺评论");
        if (comment == null) {
            return;
        }
        Glide.with(mContext)
                .load(comment.getLogo())
                .into(userLogo);
        userName.setText(comment.getNickName());
        ratingBar.setRating((int) comment.getScore(), R.drawable.rating_bar, false, 30);
        userContent.setText(comment.getContent());
        commentTime.setText(comment.getCreateTime());

        if (comment.getImages() != null && !comment.getImages().isEmpty()) {
            gridView.setAdapter(new CommenAdapter<String>(mContext, comment.getImages(), R.layout.list_comment_img_item) {
                @Override
                public void convert(com.sunday.common.adapter.ViewHolder helper, String item, int position) {
                    ImageView imgView = helper.getView(R.id.comment_img);
                    Glide.with(mContext)
                            .load(item)
                            .error(R.mipmap.ic_default)
                            .into(imgView);
                }
            });
            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(mContext, ShowBigImage.class);
                    intent.putStringArrayListExtra("data", (ArrayList<String>) comment.getImages());
                    intent.putExtra("index", position);
                    mContext.startActivity(intent);
                }
            });
        }

        if (comment.getReplay() != null && !comment.getReplay().isEmpty()) {
            llReply.setVisibility(View.VISIBLE);
            llMakeReply.setVisibility(View.GONE);
            replyContent.setText(comment.getReplay().get(0).getContent());
        } else {
            llReply.setVisibility(View.GONE);
            llMakeReply.setVisibility(View.VISIBLE);
        }
    }

    @OnClick(R.id.btn_reply)
    void onClick() {
        String content = editReply.getText().toString().trim();
        if (TextUtils.isEmpty(content)) {
            ToastUtils.showToast(mContext, "请填写回复内容");
            return;
        }
        showLoadingDialog(0);
        Call<ResultDO> call = ApiClient.getApiAdapter().replayComment(comment.getId(), BaseApp.getInstance().getMember().getSellerId(),
                content);
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                if (isFinish || response.body() == null) {
                    return;
                }
                dissMissDialog();
                if (response.body().getCode() == 0) {
                    ToastUtils.showToast(mContext, "回复成功");
                    finish();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });

    }


}
